﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ConjureBivouac2012.GameStates;
using ConjureBivouac2012.Mathematics;
using ConjureBivouac2012.Object;
using Microsoft.Xna.Framework;

namespace ConjureBivouac2012.World
{
    class SpawnTimer
    {
        private const int NBRMONSTER = 3;
        private const int SPAWNTIME = 500;
        private int nbrToSpawn = 0;
        private int timer = 0;
        private Random _random = new Random();
        private float bossEncounterTime = 0;

        public void Update(int nbrMonster)
        {
            bossEncounterTime += 0.005f;
            if (nbrMonster <= 0)
            {
                nbrToSpawn = 3;
                timer = 0;
            }
            else
            {
                timer++;

                if (timer == 499)
                {
                    nbrToSpawn = 1;
                }

                timer %= 500;
            }
        }

        public Entity[] SpawnMonsters()
        {
            Entity[] monster = new Entity[nbrToSpawn];
            if (nbrToSpawn > 0)
            {
                for (int i = 0; i < nbrToSpawn; i++)
                {
                    monster[i] = SpawnMonster();
                }
                nbrToSpawn = 0;
                return monster;
            }

            return null;
        }

        private Entity SpawnMonster()
        {
            float directionX = (float)_random.Next(-100, 100) / 100;
            float directionY = (float)_random.Next(-100, 100) / 100;

            Entity enemy = GetNewEnemy();

            if (enemy != null)
            {
                enemy.sprite.Position = GetProperSpawnPosition(enemy);
                enemy.setVelocity(new Vector2(80, 80));
                enemy.SetDirection(new Vector2(directionX, directionY));
            }

            return enemy;
        }

        private AbstractEnemy GetNewEnemy()
        {
            AbstractEnemy enemy;
            int monsterType = _random.Next(100);
            if (monsterType < bossEncounterTime)
            {
                enemy = new Boss(10, -1);
                bossEncounterTime = 0;
            }
            else if (monsterType < 40)
            {
                enemy = new BasicEnemy(4, -1);
            }
            else if (monsterType < 70)
            {
                enemy = new MonsterB(8, -1);
            }
            else if (monsterType < 90)
            {
                enemy = new MonsterC(4, -1);
            }
            else
                enemy = new BasicEnemy(4, -1);

            return enemy;
        }

        private Vector2 GetProperSpawnPosition(Entity spawn)
        {
            Vector2 pos = Vector2.Zero;

            if (spawn != null && Program.GetGameInstance.GetCurrentState() is PlayGame)
            {
                List<Entity> list = ((PlayGame) Program.GetGameInstance.GetCurrentState()).GetEntityList();

                bool foundPos = false;
                int nbTries = 0;

                while(!foundPos && nbTries < 2)
                {
                    nbTries++;
                    bool collides = false;

                    int x = _random.Next(0 + 5 + (int)spawn.GetRadius(), GameInfo.GetInstance().GetDevice().Viewport.Width - 5 - (int)spawn.GetRadius());
                    int y = _random.Next(0 + 5 + (int)spawn.GetRadius(), GameInfo.GetInstance().GetDevice().Viewport.Height - 5 - (int)spawn.GetRadius());

                    pos = new Vector2(x, y);

                    collides = Phasik.IsCollidesWithAEntity(list, pos, spawn.GetRadius());
                }
            }

            return pos;
        }

        
    }
}
